package com.cc.flyingfish.controller;

import brave.Tracer;
import com.cc.flyingfish.annotation.IgnoreResponseAdvice;
import com.cc.flyingfish.param.DemoParamEntity;
import com.cc.flyingfish.result.DemoResultEntity;
import com.cc.flyingfish.util.TokenParseUtil;
import com.cc.flyingfish.vo.LoginUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CHENCHEN
 * @title
 * @createTime 2023-02-27 21:06
 */
@Slf4j
@Api(tags = "Controller描述，例：XX控制器")
@RestController
public class DemoController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private Tracer tracer;

    //@RequestParam(defaultValue = "NACOS_CLIENT_DEMO")参数为服务名称
    @IgnoreResponseAdvice
    @GetMapping("index")
    public List<ServiceInstance> index(@RequestParam(defaultValue = "NACOS_CLIENT_DEMO") String serviceId){
        log.info("RequestParam value is : [{}] " + serviceId);
        return discoveryClient.getInstances(serviceId);
    }

    @ApiOperation(value = "获取用户信息", notes = "通过Token换取用户实体", httpMethod = "GET")
    @GetMapping("getLoginUserInfo")
    public LoginUserInfo getToken(@RequestHeader("Authorization") String authorization){
        log.info("RequestHeader value is : [{}] " + authorization);
        return TokenParseUtil.parseUserInfoFromToken(authorization);
    }

    @ApiOperation(value = "接口简述", notes = "接口功能详细描述", httpMethod = "POST")
    @PostMapping("api")
    public DemoResultEntity getToken(@RequestHeader("Authorization") String authorization,
                                     @RequestBody DemoParamEntity demoParamEntity){
        log.info("RequestHeader value is : [{}] " + authorization);
        log.info("tacerId : [{}] " + tracer.currentSpan().context().traceId());
        log.info("spanId : [{}] " + tracer.currentSpan().context().spanId());
        DemoResultEntity demoResultEntity = new DemoResultEntity(999L, null);
        return demoResultEntity;
    }
}
